import { message } from 'antd';
import { list, dele, allow, undo, listSpecial } from '../services/count';
import tools from '../../../utils';

const types = require('../../../assets/stureg_web/static/types.json');

export default {
  namespace: 'count',

  state: {
    data: [],
    total: 0,
    success: true,

    dataSpecial: [], // 开设专业数据(树数据)
    sysData: [], // 系部数据
    specData: {}, // 专业数据
    mappingSpecialName: {}, // 根据专业id获取专业名称
    mappingSpecialPID: {}, // 根据专业id获取系部id
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response });
    },
    *dele({ payload }, { call, put }) {
      const response = yield call(dele, payload);
      yield put({ type: 'callbackSave', response });
    },
    *allow({ payload }, { call, put }) {
      const response = yield call(allow, payload);
      yield put({ type: 'callbackSave', response });
    },
    *undo({ payload }, { call, put }) {
      const response = yield call(undo, payload);
      yield put({ type: 'callbackSave', response });
    },
    *listSpecial({ payload }, { call, put }) {
      const response = yield call(listSpecial, payload);
      yield put({ type: 'callbackListSpecial', response });
    },
  },

  reducers: {
    callbackList(st, { response }) {
      const state = st;
      if (response.success) {
        state.total = response.total;
        state.data = response.root
          .map((item) => {
            const { specialId, type } = item;
            let specialName = state.mappingSpecialName[specialId];
            specialName += ' - ' + types[type];

            return {
              ...item,
              specialName,
            };
          });
      }
      return { ...state };
    },
    callbackSave(st, { response }) {
      const state = st;
      state.success = response.success;
      if (response.success) message.success(response.msg);
      return { ...state };
    },
    callbackListSpecial(st, { response }) {
      const state = st;
      if (response.success) {
        const { data } = response;

        state.sysData = [];
        state.specData = {};
        const allData = [];
        const mappingSpecialName = {}; // 根据专业id获取专业名称
        const mappingSpecialPID = {}; // 根据专业id获取系部id

        for (let i = 0; i < data.length; i += 1) {
          const item = data[i];
          const { id, pid, name } = item;
          const isSpec = pid > 0; // 系部或专业: 0系部, >0专业
          mappingSpecialName[id] = name;
          if (isSpec) {
            if (!state.specData[pid]) state.specData[pid] = [];
            state.specData[pid].push(item);
            mappingSpecialName[id] = name;
            mappingSpecialPID[id] = pid;
          } else state.sysData.push(item);

          allData.push({
            ...item,
            key: id,
            title: name,
            selectable: isSpec, // 专业树节点可供用户点选, 而系部不可选择
          });
        }

        state.mappingSpecialName = mappingSpecialName;
        state.mappingSpecialPID = mappingSpecialPID;
        state.dataSpecial = tools.funcPidToChildren(allData);
      }
      return { ...state };
    },
  },
};
